<?php

namespace App\Http\Controllers\YApi;

//use Illuminate\Http\Request;
use Request;
use App\Http\Requests;
use App\Http\Controllers\Controller;

use Session;
use Redirect;

use App\Models\UsersReg;
use App\Models\UsersAttrib;
use App\Models\mall\Ecy_users; //商城会员

class UserRegController extends Controller
{

    public function userreg()
    {
		$username = request::input('username');
		$email = request::input('email');
		$mobile = request::input('mobile');
		$password = request::input('password'); 
		$sex = request::input('sex');

		$user = UsersReg::where('user_name', $username)->first();
		if($user != '')
		{
			return array('code' => -1, 'msg' => '用户名已经存在');
		}

		$user = UsersReg::where('email', $email)->first();
		if($user != '')
		{
			return array('code' => -2, 'msg' => '信箱已经存在');
		}

		$user = UsersReg::where('mobile', $mobile)->first();
		if($user != '')
		{
			return array('code' => -3, 'msg' => '手机号码已经存在');
		}

		// 注册夜读书会员
		$salt = $this->fetch_salt(4);	  
		$user = new UsersReg;
		$user->user_name = $username;
		$user->email = $email;
		$user->mobile = $mobile;
		$user->sex = $sex;
		$user->salt = $salt;
		$user->password = $this->compile_password($password, $salt);
		$user->reg_time = time();
		$user->reg_laiyuan = 'R'; // 标识会员通过rn程序注册
		$user->save(); 

		//注册会员信息添加到商城数据库中
        $ecy_users = new Ecy_users;
        $ecy_users->user_name = $username;  //用户名
        $ecy_users->email = $email;  //信箱
        $ecy_users->mobile_phone = $mobile; //手机号码
        $ecy_users->password = md5($password); //密码
        $ecy_users->reg_time = time(); //注册时间
        $ecy_users->last_login = time(); //最后登录时间
		$ecy_users->save();

		return array('user' => $user,'code'=> 0,'msg' => '用户信息注册成功');

    }


    public function fetch_salt($length = 4)
	{
		// 生成密码种子
		for ($i = 0; $i < $length; $i++)
		{
			$salt[$i] = chr(rand(97, 122));
		}

		$salt = $salt[0] . $salt[1] . $salt[2] . $salt[3]; //
		return $salt;
	}


    public function compile_password($password, $salt)
    {
		// 根据 salt 混淆密码
        $password = md5(md5($password) . $salt);
        return $password;
    }
    
    public function saveusername()
    {
    	// 修改保存用户名字
    	$username = request::input('username');
    	$uid = request::input('uid');

    	// 判断要改的名字是否存在 
    	$isuser = UsersReg::where('user_name', $username)->first();
    	if(empty($isuser)) {
    		// 如果不存在重名，则修改名字
    		$user = UsersReg::find($uid);
    		$user->user_name = $username;
    		$user->save();
    		return array(
    			'code' => 1,
    			'message' => '用户名字修改成功'
    		);
    	} else {
    		return array(
    			'code' => 2,
    			'message' => '用户名字已经存在了'
    		);
    	}
    }

    public function saveemail()
    {
    	// 修改保存信箱
    	$email = request::input('email');
    	$uid = request::input('uid');

    	$isemail = UsersReg::where('email', $email)->first();
    	if(empty($isemail)) {
    		// 如果不存在重复信箱，则修改信箱
    		$user = UsersReg::find($uid);
    		$user->email = $email;
    		$user->save();
    		return array(
    			'code' => 1,
    			'message' => '信箱修改成功'
    		);
    	} else {
    		return array(
    			'code' => 2,
    			'message' => '信箱已经存在了'
    		);
    	}
    }

    public function setmima()
    {
    	// 修改设置密码
    	$uid = request::input('uid');
    	$password = request::input('password');

    	$user = UsersReg::find($uid);
    	$salt = $this->fetch_salt(4);
    	$user->salt = $salt;
		$user->password = $this->compile_password($password, $salt);
		$user->save(); 

		return array(
			'code' => 1,
			'message' => '密码修改成功'
		);
    }

    public function savesex()
    {
    	// 修改保存设置性别
    	$uid = request::input('uid');
    	$sex = request::input('sex');

    	$user = UsersReg::find($uid);
    	$user->sex = $sex;
    	$user->save();
    	return array(
    		'code' => 1,
			'message' => '性别修改成功'
    	);
    }

    public function savecity()
    {
    	// 修改保存设置城市
    	$uid = request::input('uid');
    	$sheng = request::input('sheng');
    	$shi = request::input('shi');

    	$user = UsersReg::find($uid);
    	$user->province = $sheng;
    	$user->city = $shi;
    	$user->save();
    	return array(
    		'code' => 1,
    		'message' => '城市修改成功'
    	);
    }

    public function saveqianming()
    {
    	// 保存设置的签名
    	$uid = request::input('uid');
    	$signature = request::input('signature');

    	$user = UsersAttrib::where('uid', $uid)->first();

    	$user->signature = $signature;
    	$user->save();

    	return array(
    		'code' => 1,
    		'message' => '用户签名修改成功'
    	);
    }

    public function saveshengri()
    {
    	// 保存生日的生日
    	$uid = request::input('uid');
    	$birthday = request::input('birthday');

    	$user = UsersReg::find($uid);
    	$user->birthday = $birthday;
    	$user->save();

    	return array(
    		'code' => 1,
    		'message' => '用户生日修改成功'
    	);

    }
}
